package cn.com.nthotline.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import cn.com.nthotline.R;
import cn.com.nthotline.db.DBCategory;
import cn.com.nthotline.model.Information;

public class PublishedEditAdapter extends EnhancedAdapter<Information> {
	private DBCategory db;
	public static Map<Integer, String> map;

	public PublishedEditAdapter(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		db=new DBCategory(context);
		map=new HashMap<Integer, String>();
	}
	public void addAll(List<Information> list){
		this.dataList=list;
		notifyDataSetChanged();
	}
	@Override
	protected void bindView(View paramView, Context paramContext, final int position) {
		// TODO Auto-generated method stub
		final Information info = getItem(position);
		String catName=db.getCatName(info.getCatId());
		ViewHolder holder = (ViewHolder) paramView.getTag();
		holder.title.setText(info.getTitle());
		holder.category.setText(catName);
		holder.chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				dataList.get(position).setChecked(isChecked);
				if(isChecked){
					map.put(position, info.getId());
					System.out.println("edit item id:"+info.getId());
				}else{
					map.remove(position);
				}
			}
		});
		holder.chk.setChecked(dataList.get(position).isChecked());
	}

	@Override
	protected View newView(Context context, int i, ViewGroup viewgroup) {
		// TODO Auto-generated method stub
		View view = LayoutInflater.from(mContext).inflate(
				R.layout.published_check_item, null);

		ViewHolder holder = new ViewHolder();
		holder.title = (TextView) view.findViewById(R.id.tvTitle);
		holder.category=(TextView)view.findViewById(R.id.tvCategory);
		//holder.btnReflesh=(Button)view.findViewById(R.id.btnReflesh);
		holder.chk=(CheckBox)view.findViewById(R.id.delete);
		view.setTag(holder);
		return view;
	}

	public static class ViewHolder {
		public TextView title,category;
		CheckBox chk;
	}
}
